/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.lehe.common.exceptions.LHServiceException;
import com.lehe.entry.wx.message.ReqBaseMessage;
import com.lehe.entry.wx.message.RespBaseMessage;
import com.lehe.service.IHttpSendService;
import com.lehe.service.ITokenService;
import com.lehe.service.IWeChatExecuteService;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 微信通讯抽象<br>
 *
 * @author 王丹
 * @FileName AbstractWeChatExecuteService.java<br>
 * @Language Java<br>
 * @date 2016-01-08<br>
 */
public abstract class AbstractWeChatExecuteService<T> implements IWeChatExecuteService<T> {

    @Autowired
    protected IHttpSendService httpSendService;
    @Autowired
    protected ITokenService tokenService;

    protected ObjectMapper mapper = new ObjectMapper();

    protected List<NameValuePair> buildParams(Map<String, String> reqMessage){
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        if(null!=reqMessage && !reqMessage.isEmpty()){
            for(String key : reqMessage.keySet()){
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, reqMessage.get(key));
                pairs.add(basicNameValuePair);
            }
        }
        return pairs;
    }

    protected String buildParamsJson(ReqBaseMessage reqMessage) throws LHServiceException {
        try {
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return mapper.writeValueAsString(reqMessage);
        } catch (IOException e) {
            e.printStackTrace();
            throw new LHServiceException("转换请求报文异常");
        }
    }

    protected RespBaseMessage convertRespJson(String respHtml, Class<? extends RespBaseMessage> clazz) throws LHServiceException{
        try {
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return mapper.readValue(respHtml, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            throw new LHServiceException("转换响应报文异常");
        }
    }

    protected abstract String queryRequestUrl() throws LHServiceException;

    protected abstract RespBaseMessage convertRespMessage(String respHtml) throws LHServiceException;

}
